package cn.wolfcode.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    //查询列表
    //@RequiredPermission(name="角色列表",expression = "role:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        model.addAttribute("pageInfo",roleService.query(qo));
        return "/role/list";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list.do";
    }

    //添加和修改跳转页面
    @RequestMapping("/input")
    public String input(Model model,Long id){
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);
        if (id != null) {
            Role role = roleService.get(id);
            model.addAttribute("role",role);
        }
        return "/role/input";
    }

    //添加和修改
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        if (role.getId() != null) {
            roleService.update(role,ids);
        }else{
            //添加
            roleService.save(role,ids);
        }
        return "redirect:/role/list.do";
    }

}
